﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;
using EntitySpaces.Interfaces;

public partial class Admin_SAPFin_SAPFin005b : AdminBasePage
{
    protected decimal? boxId;
    protected string companyCode = string.Empty;
    protected string companyName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["BoxId"] != null)
        {
            boxId = decimal.Parse(Request.QueryString["BoxId"]);
        }
        if (Request.QueryString["companyCode"] != null)
        {
            companyCode = Request.QueryString["companyCode"].ToString();
        }
        if (Request.QueryString["companyName"] != null)
        {
            companyName = Request.QueryString["companyName"].ToString();
        }


        if (!IsPostBack)
        {
            drpCompany.SelectedValue = companyName + "-" + companyCode;
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["Dsd"] != null)
            {
                txtDocumentStartDate.Value = Session["Dsd"].ToString();
            }
            if (Session["Ded"] != null)
            {
                txtDocumentEndDate.Value = Session["Ded"].ToString();
            }
            if (Session["Ssd"] != null)
            {
                txtStoreStartDate.Value = Session["Ssd"].ToString();
            }
            if (Session["Sed"] != null)
            {
                txtStoreEndDate.Value = Session["Sed"].ToString();
            }

            if (Session["drpDocType"] != null)
            {
                drpDocType.SelectedValue = Session["drpDocType"].ToString();
            }
            if (Session["txtKey"] != null)
            { txtKey.Text = Session["txtKey"].ToString(); }

            if (Session["drpFunctionDept"] != null)
            {
                drpFunctionDept.SelectedValue = Session["drpFunctionDept"].ToString();
            }
            if (Session["txtUserName"] != null)
            { txtUserName.Text = Session["txtUserName"].ToString(); }
            if (Session["txtDocumentsType"] != null)
            { txtDocumentsType.Text = Session["txtDocumentsType"].ToString(); }
            if (Session["txtReferenceTransact"] != null)
            { txtReferenceTransact.Text = Session["txtReferenceTransact"].ToString(); }
            if (Session["txtDocumentSystem"] != null)
            { txtDocumentSystem.Text = Session["txtDocumentSystem"].ToString(); }
            if (Session["txtLogicalSystem"] != null)
            { txtLogicalSystem.Text = Session["txtLogicalSystem"].ToString(); }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //Session["txtBoxNo"] = txtBoxNo.Text.Trim();
        //Session["txtSupplierBoxNo"] = txtSupplierBoxNo.Text.Trim();
        Session["drpDocType"] = drpDocType.SelectedValue;
        Session["txtKey"] = txtKey.Text.Trim();
        Session["drpCompany"] = drpCompany.SelectedValue;
        Session["drpFunctionDept"] = drpFunctionDept.SelectedValue;
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["Dsd"] = txtDocumentStartDate.Value;
        Session["Ded"] = txtDocumentEndDate.Value;
        Session["Ssd"] = txtStoreStartDate.Value;
        Session["Sed"] = txtStoreEndDate.Value;
        Session["txtUserName"] = txtUserName.Text.Trim();
        Session["txtDocumentsType"] = txtDocumentsType.Text.Trim();
        Session["txtReferenceTransact"] = txtReferenceTransact.Text.Trim();
        Session["txtDocumentSystem"] = txtDocumentSystem.Text.Trim();
        Session["txtLogicalSystem"] = txtLogicalSystem.Text.Trim();
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinReceiptQuery q1 = new FinReceiptQuery("q1");


        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFCompanyDicQuery q7 = new BFCompanyDicQuery("q7");
        VReceiptExtInfoQuery q8 = new VReceiptExtInfoQuery("q8");

        TSCodeTableQuery q9 = new TSCodeTableQuery("q9");



        q1.LeftJoin(q9).On(q1.VoucherDescription == q9.Code);

        q1.LeftJoin(q2).On(q1.FunctionDept == q2.Code);
        q1.LeftJoin(q3).On(q1.StorageStatus == q3.Code);
        q1.LeftJoin(q4).On(q1.VoucherDescription == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageLocation == q5.ThirdUnitID);
        q1.LeftJoin(q6).On(q1.Division == q6.Code);
        q1.LeftJoin(q7).On(q1.CompanyCode == q7.CompanyCode);
        q1.LeftJoin(q8).On(q1.ReceiptID == q8.ReceiptID);


        q1.SelectAll().Select(q2.Value1.As("FunctionDeptStr"), q3.Value1.As("StorageStatusStr"), q4.Value1.As("VoucherDescriptionName"), q5.Name.As("StorageLocationStr"), q6.Value1.As("DivisionStr"), q7.CompanyName, q7.CompanyCode, q8.SupplierBoxNo, q8.BoxNo, q9.Value1.As("DocDesc"));

        ArrayList args = new ArrayList();
        ArrayList argsOr = new ArrayList();
        args.Add(q1.BoxID == boxId);

        if (!string.IsNullOrEmpty(drpDocType.SelectedValue))
        {
            args.Add(q1.VoucherDescription == drpDocType.SelectedValue);
        }
        if (!string.IsNullOrEmpty(txtKey.Text.Trim()))
        {
            argsOr.Add(q1.DocumentsNo.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.Reference.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.ReferenceKey.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.DocumentHeaderText.Like("%" + txtKey.Text.Trim() + "%"));
            args.Add(q1.Or(argsOr.ToArray()));
        }
        if (!string.IsNullOrEmpty(drpFunctionDept.SelectedValue))
        {
            args.Add(q1.FunctionDept == drpFunctionDept.SelectedValue);
        }
        if (!string.IsNullOrEmpty(txtDocumentStartDate.Value))
        {
            args.Add(q1.VoucherDate >= txtDocumentStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtDocumentEndDate.Value))
        {
            args.Add(q1.VoucherDate <= DateTime.Parse(txtDocumentEndDate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtStoreStartDate.Value))
        {
            args.Add(q1.FirstStorageDate >= txtStoreStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtStoreEndDate.Value))
        {
            args.Add(q1.FirstStorageDate <= DateTime.Parse(txtStoreEndDate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtStartDate.Value))
        {
            args.Add(q1.CreatedDttm >= txtStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtEndate.Value))
        {
            args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
        {
            args.Add(q1.UserName.Like("%" + txtUserName.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtDocumentsType.Text.Trim()))
        {
            args.Add(q1.DocumentsType.Like("%" + txtDocumentsType.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtReferenceTransact.Text.Trim()))
        {
            args.Add(q1.ReferenceTransact.Like("%" + txtReferenceTransact.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtDocumentSystem.Text.Trim()))
        {
            args.Add(q1.DocumentSystem.Like("%" + txtDocumentSystem.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtLogicalSystem.Text.Trim()))
        {
            args.Add(q1.LogicalSystem.Like("%" + txtLogicalSystem.Text.Trim() + "%"));
        }

        q1.Where(args.ToArray()).OrderBy(q1.DocumentsNo.Ascending);
        q1.es.Top = 1000;
        DataTable tableSorce = q1.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("drpDocType");
        Session.Remove("txtKey");
        Session.Remove("drpCompany");
        Session.Remove("drpFunctionDept");
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("Dsd");
        Session.Remove("Ded");
        Session.Remove("Ssd");
        Session.Remove("Sed");
        Session.Remove("txtUserName");
        Session.Remove("txtDocumentsType");
        Session.Remove("txtReferenceTransact");
        Session.Remove("txtDocumentSystem");
        Session.Remove("txtLogicalSystem");
    }



    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (boxId.HasValue && (!string.IsNullOrEmpty(HiddenFieldIds.Value)))
        {
            string[] idList = HiddenFieldIds.Value.Substring(0, HiddenFieldIds.Value.Length - 1).Split(',');
            using (esTransactionScope tr = new esTransactionScope())
            {
                DateTime now = DateTime.Now;
                string currentUser = LoginUserInfo.CurrentUser.UserName;
                FinBoxReceiptsCollection finBoxReceiptsCollection = new FinBoxReceiptsCollection();
                ArrayList args = new ArrayList();
                ArrayList argsOr = new ArrayList();


                for (int i = 0; i < idList.Length; i++)
                {
                    FinReceipt finReceipt = new FinReceipt();
                    finReceipt.LoadByPrimaryKey(decimal.Parse(idList[i]));
                    finReceipt.BoxID = -1;
                    finReceipt.Save();
                    argsOr.Add(finBoxReceiptsCollection.Query.ReceiptID == decimal.Parse(idList[i]));
                }
                
                args.Add(finBoxReceiptsCollection.Query.BoxID == boxId);
                if (argsOr.Count > 0)
                { args.Add(finBoxReceiptsCollection.Query.Or(argsOr.ToArray())); }
                finBoxReceiptsCollection.Query.Where(args.ToArray());
                finBoxReceiptsCollection.Query.Load();
                finBoxReceiptsCollection.MarkAllAsDeleted();
                finBoxReceiptsCollection.Save();

                FinBox finBox = new FinBox();
                finBox.LoadByPrimaryKey(boxId.Value);
                finBox.StorageStatus = "S99";
                finBox.Save();

                tr.Complete();

                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('已经成功将凭证移除出凭证盒！');", true);
            }
        }
    }
}